/**
 * 2018年12月6日下午1:55:19
 * @author 谢进财
 */
package com.baihang.platform.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.kit.ShiroKit;
import com.baihang.framework.service.BaseService;
import com.baihang.project.ds.dao.EmployeeDao;
import com.baihang.project.ds.entity.MenuEntity;

/**
 * 主页服务
 *
 */
@Service
public class IndexService extends BaseService {
	@Autowired
	private EmployeeDao dao;
	public List<MenuEntity> getMenuTree() {
		String sql = "select * from sys_menu where state =0 order by Menu_order ";
		if(!"admin".equals(ShiroKit.getCurrentUser().getUserName())) {
			String menus = ShiroKit.getCurrentUser().getMenus();
			sql = String.format("select * from sys_menu where Menu_id in (%s) and state =0 order by Menu_order", menus);
		}
		List<MenuEntity>  list =  dao.getMenuEntityList(sql);
		List<MenuEntity>  list2 = list.stream().filter(p->p.getParentid()==0).collect(Collectors.toList());
		List<MenuEntity>  jsonEntity = new ArrayList<MenuEntity>();
		List<MenuEntity>  children = new ArrayList<MenuEntity>();
		for(MenuEntity  menu : list2) {
			children = list.stream().filter(p->p.getParentid()==menu.getId()).collect(Collectors.toList());
			menu.setChildren(children);
			jsonEntity.add(menu);
		}
		return jsonEntity;
	} 
}
